﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using pTools.cMethods;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;



namespace pTools.TuQiangPrint
{
    public partial class SelectWaterWash : DevExpress.XtraEditors.XtraForm
    {
        DBHelper db = new DBHelper();

        List<DbShuiXiBiaoInfo> list { get; set; }

        public List<DbShuiXiBiaoInfo> selectedList { get; set; }

        public SelectWaterWash()
        {
            InitializeComponent();

            selectedList = new List<DbShuiXiBiaoInfo>();
        }

        private void SelectWaterWash_Load(object sender, EventArgs e)
        {
            LoadWaterWashList();
        }

        void LoadWaterWashList()
        {
            var sql = @"select * from 
fun_ShuiXiBiao_Info where isDel = 0 ";


            var waterWashCode = tb_waterWashCode.Text;
            var proCode = tb_proCode.Text;
            var proName = tb_proName.Text;
            var color = tb_color.Text;
            var trademark = tb_trademark.Text;

            if (!string.IsNullOrEmpty(waterWashCode))
            {
                sql += " and outbarcode like '%" + waterWashCode + "%' ";
            }
            if (!string.IsNullOrEmpty(proCode))
            {
                sql += " and procode like '%" + proCode + "%' ";
            }
            if (!string.IsNullOrEmpty(proName))
            {
                sql += " and proName like '%" + proName + "%' ";
            }
            if (!string.IsNullOrEmpty(color))
            {
                sql += " and proColor like '%" + color + "%' ";
            }
            if (!string.IsNullOrEmpty(trademark))
            {
                sql += " and brand like '%" + trademark + "%' ";
            }

            var dt = db.getData(sql);

            list = CommonMethods.ConvertTo<DbShuiXiBiaoInfo>(dt);

            grid_waterwash.DataSource = list;
        }

        private void textEdit_EditValueChanged(object sender, EventArgs e)
        {
            LoadWaterWashList();
        }

        private void gridView_waterWash_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            //userClickedRowModel = list[gridView_waterWash.FocusedRowHandle];

            Point pt = grid_waterwash.PointToClient(Control.MousePosition);
            GridHitInfo info = gridView_waterWash.CalcHitInfo(pt);

            if (gridView_waterWash.IsRowSelected(e.RowHandle) && info.Column.VisibleIndex != 0)
            {
                gridView_waterWash.UnselectRow(e.RowHandle);
            }
            else if (!gridView_waterWash.IsRowSelected(e.RowHandle) && info.Column.VisibleIndex != 0)
            {
                gridView_waterWash.SelectRow(e.RowHandle);
            }
        }

        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            selectedList = list.Where(
                r =>
                    gridView_waterWash.GetSelectedRows()
                    .Contains(list.IndexOf(r)))
                    .ToList();

            this.Close();
        }

    }
}